package org.itboys.mobile.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayFloorMrGroup;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/7/18
 * @Description：
 */
@Service
public class SundayFloorMrGroupService extends BaseMongoService<SundayFloorMrGroup>{

    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }
    @Override
    protected Class<SundayFloorMrGroup> getEntityClass() {
        return SundayFloorMrGroup.class;
    }

    @Autowired
    private SundayUploadService uploadService;



    /**
     * 保存或是新增
     * @param id
     * @param title
     * @param desc
     * @param imageFile
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(Long id, String title,String subTitle,String link,MultipartFile imageFile) throws IOException {
        Map<String, String> map =uploadService.uploadSingle(imageFile);
        String image="";
        if(StringUtils.isNotEmpty(map.get(SundayUploadService.UPLOAD_HALF_PATH))){
            image = map.get(SundayUploadService.UPLOAD_HALF_PATH);
        }
        SundayFloorMrGroup group=new SundayFloorMrGroup( image, title, subTitle,link);
        if(LongUtil.isNotZreo(id)){
            super.updateExceptEmpty(id,group);
        }else {
            super.save(group);
        }
    }
    /**
     * 分页查询所有楼先生问题组
     * @param request
     * @return
     */
    public  PageResult<SundayFloorMrGroup> select(HttpServletRequest request){

        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request, "title");
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);
        return super.containpageQuery(request,vagueParam,param);
    }

    public List<SystemDictionary> getDictionary(){
        List<SystemDictionary> result=new ArrayList<>();
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", 0);
        List<SundayFloorMrGroup> groups = super.list(param);
        if(ListUtil.isNotNull(groups)){
            result= packageDictionary(groups);
        }
        return result;
    }
    public List<SystemDictionary> packageDictionary(List<SundayFloorMrGroup> groups){
        List<SystemDictionary> result=new ArrayList<>();
        for ( SundayFloorMrGroup group:groups) {
            SystemDictionary dictionary=new SystemDictionary(null,group.getTitle(),group.getId()+"",null);
            result.add(dictionary);
        }
        return result;
    }
}
